package com.barks.learning.gateway;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenw-i
 * @version 1.0.0
 * @ClassName UserApplication.java
 * @Description TODO
 * @createTime 2020年07月11日 20:20:00
 */
@EnableDiscoveryClient
@SpringBootApplication
@RestController
@RefreshScope
public class UserApplication {

    private Logger logger = LoggerFactory.getLogger(UserApplication.class);

    @Value("${aa.bb:0}")
    private int param;

    @PostMapping("/user/create")
    public String createUser(User user){
        logger.info("create user {}",user);
        return "user create success";
    }

    /**
     * 针对这个 API 进行限流
     * @return
     */
    @GetMapping("/user/get")
    public User get(){
        logger.info("/user/get = > param:{}",param);
        return new User();
    }

    @GetMapping("/user/params")
    public String get1(){
        logger.info("/user/get1 = > param:{}",param);
        return param + "";
    }

    @GetMapping("/user/timeout2000")
    public String timeout2000(){
        logger.info("/user/timeout2000 ");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "time out 2000";
    }

    @GetMapping("/user/timeout10000")
    public String timeout10000(){
        logger.info("/user/timeout10000 ");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "time out 10000";
    }

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}
